package com.zyj.myzhxy.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @ClassName: UploadUtil
 * @Description: TODO
 * @Author: 杨振坤
 * @date: 2023/4/18 22:12
 */
public class UploadUtil {
    /*获取域名*/
    public static final String ALI_DOMAIN = "https://yzk-springboot.oss-cn-beijing.aliyuncs.com/";

    public static String uploadImage(MultipartFile file) throws IOException {
        /*生成文件名*/
        String originalFilename = file.getOriginalFilename();
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + ext;

        /*地域节点*/
        String endPoint = "https://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5tSdUtA7iD6TuYcars8J";
        String accessKaySecret = "hLiJbWLY4tptRj3SJ4VhF5UwwXGViP";

        /*OSS客户端对象*/
        OSS ossClient=new OSSClientBuilder().build(endPoint,accessKeyId,accessKaySecret);
        ossClient.putObject(
                "yzk-springboot",/*仓库名*/
                fileName,/*文件名*/
                file.getInputStream()
        );
        ossClient.shutdown();

        return ALI_DOMAIN+fileName;
    }
}
